using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class PlanogramsRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class PlanogramsDbo : DatabaseObject
        {
            public PlanogramsDbo()
            {
                Name = "Planograms";
                GeneratedClassName = "Planograms";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "RangeReviewId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "RangeReviewId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreId", ColumnType = "smallint", IsIdentity = false, IsNullable = false, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "StoreId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Name", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Name" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Modules", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Modules" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Width", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "Width" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SquareMeters", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "SquareMeters" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Levels", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Levels" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PlanogramLocationId", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "PlanogramLocationId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Level1", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Level1" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Level2", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Level2" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Level1Space", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Level1Space" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "TypeId", ColumnType = "tinyint", IsIdentity = false, IsNullable = true, Length = 1, Precision = 3, Scale = 0, GeneratedPropertyName = "TypeId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ListingPlanogramId", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "ListingPlanogramId" });
              
            }
        }
		
		public static List<PlanogramRow> Select(Expression<Func<PlanogramRow, bool>> expression)
        {
            List<PlanogramRow> rows = new List<PlanogramRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<PlanogramRow>(new PlanogramsDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<PlanogramRow> Select()
		{
			List<PlanogramRow> rows = new List<PlanogramRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<PlanogramRow>(new PlanogramsDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<PlanogramRow> PagedSelect(Expression<Func<PlanogramRow, bool>> filterExpression, Expression<Func<PlanogramRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<PlanogramRow> rows = new List<PlanogramRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<PlanogramRow>(new PlanogramsDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<PlanogramRow> PlanogramList)
        {
            PlanogramRow row;

            while (reader.Read())
            {
                row = new PlanogramRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					RangeReviewId = Util.CastDbValue<int>(reader["RangeReviewId"]),
					StoreId = Util.CastDbValue<int>(reader["StoreId"]),
					Name = Util.CastDbValue<string>(reader["Name"]),
					Modules = Util.CastDbValue<int?>(reader["Modules"]),
					Width = Util.CastDbValue<decimal?>(reader["Width"]),
					SquareMeters = Util.CastDbValue<decimal?>(reader["SquareMeters"]),
					Levels = Util.CastDbValue<int?>(reader["Levels"]),
					PlanogramLocationId = Util.CastDbValue<int?>(reader["PlanogramLocationId"]),
					Level1 = Util.CastDbValue<string>(reader["Level1"]),
					Level2 = Util.CastDbValue<string>(reader["Level2"]),
					Level1Space = Util.CastDbValue<decimal?>(reader["Level1Space"]),
					TypeId = Util.CastDbValue<int?>(reader["TypeId"]),
					ListingPlanogramId = Util.CastDbValue<int?>(reader["ListingPlanogramId"])
                };
                                
                PlanogramList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(PlanogramRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<PlanogramRow>(new PlanogramsDbo(), row);
            }
        }

		public static void Update(PlanogramRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<PlanogramRow>(new PlanogramsDbo(), row);
            }
        }

		public static void Delete(Expression<Func<PlanogramRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<PlanogramRow>(new PlanogramsDbo(), expression);
            }
        }

	}
}
	
